﻿//using System;
//using System.Collections.Generic;
//using System.Windows;
//using System.Windows.Interactivity;
//using nRoute.Components.Bindings;
//using nRoute.Utilities;
//using System.Windows.Data;

//namespace nRoute.Components
//{
//    public partial class DependencyParameter
//        : IAttachedObject
//    {
//        private const string DEPENDENCYOBJECT_CAN_SETONCE = "DependencyObject can only be set once";
//        private const string DEPENDENCYOBJECT_MUSTBE_FRAMEWORK = "Attached DependencyObject must be FrameworkElement derivative";

//        private FrameworkElement _associatedObject;
//        private Dictionary<DependencyProperty, BindingInfo> _pendingBindings;

//#region IAttachedObject related

//        DependencyObject IAttachedObject.AssociatedObject
//        {
//            get { return _associatedObject; }
//        }

//        void IAttachedObject.Attach(DependencyObject dependencyObject)
//        {
//            Guard.ArgumentNotNull(dependencyObject, "dependencyObject");
//            Guard.ArgumentValue(_associatedObject != null, "dependencyObject", DEPENDENCYOBJECT_CAN_SETONCE);
//            Guard.ArgumentIsType(dependencyObject, typeof(FrameworkElement), "dependencyObject");

//            _associatedObject = (FrameworkElement)dependencyObject;
//            this.OnAttached();
//            this.ApplyAllPendingBindings();
//        }

//        void IAttachedObject.Detach()
//        {
//            this.OnDetaching();
//            this.ClearAllBindings();
//            _associatedObject = null;
//        }

//#endregion

//#region Binding Helpers

//        protected void SetBinding<P>(DependencyProperty bindingProperty, Binding value)
//        {
//            SetBinding(bindingProperty, typeof(P), value);
//        }

//        protected void SetBinding(DependencyProperty bindingProperty, Type bindingType, Binding value)
//        {
//            if (_associatedObject != null)
//            {
//                this.SetAttachedBinding(_associatedObject, bindingProperty, bindingType, value);
//            }
//            else
//            {
//                if (_pendingBindings == null)
//                    _pendingBindings = new Dictionary<DependencyProperty, BindingInfo>();
//                _pendingBindings.Add(bindingProperty,
//                    new BindingInfo() { BindingProperty = bindingProperty, BindingType = bindingType, Binding = value });
//            }
//        }

//        protected Binding GetBinding(DependencyProperty bindingProperty)
//        {
//            if (_associatedObject != null)
//            {
//                return this.GetAttachedBinding(bindingProperty);
//            }
//            else if (_pendingBindings != null && _pendingBindings.ContainsKey(bindingProperty))
//            {
//                return _pendingBindings[bindingProperty].Binding;
//            }
//            else
//            {
//                return null;
//            }
//        }

//        protected void ApplyAllPendingBindings()
//        {
//            if (_pendingBindings == null || _pendingBindings.Count == 0) return;
         
//            // set all bindings
//            foreach (var _bindingInfo in _pendingBindings.Values)
//            {
//                this.SetAttachedBinding(_associatedObject, _bindingInfo);
//            }

//            // clear the pending
//            _pendingBindings.Clear();
//            _pendingBindings = null;
//        }

//        protected void ClearAllBindings()
//        {
//            if (_pendingBindings != null) _pendingBindings = null;
//            this.ClearAttachedBindings();
//        }

//#endregion

//#region Overridable

//        protected virtual void OnAttached() { }

//        protected virtual void OnDetaching() { }

//#endregion

//    }
//}
